<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-04-28 11:06:50
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-04-28 11:09:46
 -->
<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增Action' : '编辑Action'" width="650px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="130px">
      <el-form-item label="行为名称" prop="name">
        <el-input v-model="form.name" style="width: 320px;"/>
      </el-form-item>
      <el-form-item label="行为标志" prop="item">
        <el-input v-model="form.item" style="width: 320px;"/>
      </el-form-item>
      <el-form-item label="是否允许打卡" >
        <el-switch
          v-model="form.is_punch"
          active-text="是"
          inactive-text="否"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <template v-if="form.is_punch == 1">
      <el-form-item label="是否限制打卡时间" >
        <el-switch
          v-model="form.time_limit"
          active-text="限制"
          inactive-text="不限"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item v-if="form.time_limit == 1" label="打卡时间" >
            <el-col :span="11">
                <el-time-select
                  placeholder="起始时间"
                  v-model="form.start"
                  :picker-options="{
                    start: '05:00',
                    step: '00:30',
                    end: '20:00'
                  }">
                </el-time-select>
            </el-col>
            <el-col :span="11">
              <el-time-select
                  placeholder="结束时间"
                  v-model="form.end"
                  :picker-options="{
                    start: '07:00',
                    step: '00:30',
                    end: '23:30',
                    minTime: form.start
                  }">
                </el-time-select>
            </el-col>
      </el-form-item>
      <el-form-item label="未打卡是否提示" >
        <el-switch
          v-model="form.is_prompt"
          active-text="提示"
          inactive-text="不提示"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item label="是否赠送积分" >
        <el-switch
          v-model="form.is_give_score"
          active-text="赠送"
          inactive-text="不送"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item v-if="form.is_give_score == 1" label="打卡分值">
        <el-input-number v-model="form.score" :min="1" :max="100" label="打卡分值"></el-input-number>
      </el-form-item>
      <el-form-item label="是否开启评论" >
        <el-switch
          v-model="form.is_comment"
          active-text="开启"
          inactive-text="关闭"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item v-if="form.is_comment == 1" label="是否开启评分" >
        <el-switch
          v-model="form.is_score"
          active-text="开启"
          inactive-text="关闭"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <!--<el-form-item v-if="form.is_score == '1'"  label="评分比率">
        <el-input v-model="form.score_rate" style="width: 320px;"/>
      </el-form-item>-->
      <el-form-item label="打卡示例图">
        <el-upload
          class="avatar-uploader"
          action="http://wwx.api.local/admin/v1/upload/image"
          :show-file-list="false"
          :on-success="sampleSuccess"
          :before-upload="beforeUpload">
          <img v-if="form.sample" :src="form.sample" class="avatar" style="weight:100Px;height:100px">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="领取积分音乐">
        <el-upload
          class="avatar-uploader"
          action="http://wwx.api.local/admin/v1/upload/image"
          :show-file-list="false"
          :on-success="collectMusicSuccess"
          :before-upload="beforeUpload">
          <video v-if="form.collect_music"  style="width: 160px; height: 60px" :src="form.collect_music" class="video" controls>
          </video>
          <template v-else>
            <el-button size="small" type="primary">点击上传音频</el-button>
            <div slot="tip" class="el-upload__tip">音频文件不超过2M</div>
          </template>
        </el-upload>
      </el-form-item>
      <el-form-item label="打卡图标">
        <el-upload
          class="avatar-uploader"
          action="http://wwx.api.local/admin/v1/upload/image"
          :show-file-list="false"
          :on-success="PunchIconSuccess"
          :before-upload="beforeUpload">
          <img v-if="form.punch_icon" :src="form.punch_icon" class="avatar" style="weight:100Px;height:100px">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      </template>
      <el-form-item label="无订单是否加锁" >
        <el-switch
          v-model="form.is_lock"
          active-text="加锁"
          inactive-text="不加"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item label="建议/说明">
       <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入建议内容"
          v-model="form.description"
          style="width: 320px;">
          </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'Form',
  components: { Treeselect },
  props: {
    roles: {
      type: Array,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dialog: false,
      loading: false,
      form: { id: '', name: '', item: '', is_punch: 1, time_limit: 0, start: '', end: '', is_give_score: 1,
        score: 5, is_comment: 0, is_score: 0, score_rate: '', punch_icon: '', description: '', is_prompt: 1,
        is_lock: '0', collect_music: '', sample: '' },
      rules: {
        name: [
          { required: true, message: '请输入Action名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        item: [
          { required: true, message: '请输入Action对应的item', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          const _this = this
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      this.$api.permission.actionConfig.add({
        data: this.form
      }).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          message: 'tab添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      this.$api.permission.actionConfig.edit({
        data: this.form
      }).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.roleIds = []
      this.form = { id: '', name: '', item: '', is_punch: 1, time_limit: 0, start: '', end: '', is_give_score: 1,
        score: 5, is_comment: 0, is_score: 0, score_rate: '', punch_icon: '', description: '', is_prompt: 1,
        is_lock: 0, collect_music: '', sample: '' }
        console.log(this.form.time_limit)
    },
    PunchIconSuccess(res) {
      if (res.code == 200) {
        this.form.punch_icon = res.data.url;
      }
    },
    collectMusicSuccess(res) {
      this.form.collect_music = res.data.url;
  },
    sampleSuccess(res) {
      if (res.code == 200) {
        this.form.sample = res.data.url;
      }
    },
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
         this.$notify({
          title: '上传头像图片大小不能超过 2MB!',
          type: 'error',
          duration: 2500
        })
      }
      return isLt2M;
    }


  }
}
</script>

<style scoped>

</style>
